package com.daikuan.fffkuaiban.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.daikuan.fffkuaiban.adapter.HomeListAdapter2;
import com.jarhead.common.commonutils.ToastHelper;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
import com.daikuan.fffkuaiban.R;
import com.daikuan.fffkuaiban.adapter.YouxuanDaikuanListAdapter;
import com.daikuan.fffkuaiban.base.ToolBarActivity;
import com.daikuan.fffkuaiban.bmobBean.ListConfig;

import org.json.JSONArray;

import butterknife.Bind;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class YouXuanActivity extends ToolBarActivity {

	@Bind(R.id.listView)
	ListView listView;
	HomeListAdapter2 adapter;
	@Bind(R.id.refresh)
	TwinklingRefreshLayout refreshLayout;

	public static void jump(String type, String name, Activity activity) {
		Intent intent = new Intent(activity, YouXuanActivity.class);
		intent.putExtra("type", type);
		intent.putExtra("name", name);
		activity.startActivity(intent);
	}

	@Override
	public int getLayoutId() {
		return R.layout.activity_you_xuan;
	}

	@Override
	public void initPresenter() {

	}

	@Override
	public void initView() {
		toolbar.setTilte(getIntent().getStringExtra("name"));
		SinaRefreshView headerView = new SinaRefreshView(this);
		headerView.setArrowResource(R.mipmap.arrow);
		headerView.setTextColor(0xff745D5C);
		refreshLayout.setHeaderView(headerView);

		refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
			@Override
			public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
				getList();
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						refreshLayout.finishRefreshing();
					}
				}, 2000);
			}

		});

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				ListConfig listConfig = adapter.getItem(i);
				WebViewActivity.skip(YouXuanActivity.this, listConfig.channel_gourl, listConfig.channel_name);
			}
		});

	}

	@Override
	public void initData() {
		getList();
	}


	private void getList() {

		BmobQuery query = new BmobQuery("list_config");
		query.order("createdAt");
		query.addWhereEqualTo("list_id", getIntent().getStringExtra("type"));
		//v3.5.0版本提供`findObjectsByTable`方法查询自定义表名的数据
		query.findObjectsByTable(new QueryListener<JSONArray>() {
									 @Override
									 public void done(JSONArray ary, BmobException e) {
										 try {
											 if (e == null) {
												 String str = ary.toString();
												 Log.e("XXX-list", str);
												 if (JSON.parseArray(ary.toString(), ListConfig.class).size() == 0) {
													 ToastHelper.toast("暂无数据");
													 return;
												 }
												 adapter = new HomeListAdapter2(YouXuanActivity.this, JSON.parseArray(ary.toString(), ListConfig.class));
												 listView.setAdapter(adapter);
											 } else {
												 ToastHelper.toast("当前网络不佳，请下拉刷新");
											 }
										 } catch (Exception e2) {
											 ToastHelper.toast("当前网络不佳，请下拉刷新");
										 }
									 }
								 }

		);

	}


	@Override
	public void onRightClick() {

	}
}
